package com.sunriz.controller;

import com.sunriz.domain.SpaProperty;
import com.sunriz.domain.SpaResult;
import com.sunriz.mapper.CtrlMapper;
import com.sunriz.spa.SpaUtils;
import lombok.extern.slf4j.Slf4j;
import net.e175.klaus.solarpositioning.AzimuthZenithAngle;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.core.annotation.Order;
import org.springframework.data.redis.core.HashOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.List;

/**
 * ClassName: RunnerTest
 * Package: com.sunriz.controller
 * Description:
 *
 * @Author BuTang
 * @Create 2023/11/28 10:43
 * @Version 1.0
 */
@Component
@Order(1)
@Slf4j
public class Runner implements CommandLineRunner {
    @Autowired
    private RedisTemplate redisTemplate;
    @Autowired
    private CtrlMapper ctrlMapper;

    @Override
    public void run(String... args) throws Exception {
        log.info("spring boot应用启动完成，将mysql中的数据写入到redis中：");

        double start = System.currentTimeMillis();
        List<SpaResult> spareSult = ctrlMapper.select();

        log.info("查询计算spa需要数据耗时："+(System.currentTimeMillis()-start));
        start = System.currentTimeMillis();
        LocalDateTime time = LocalDateTime.now();

        for (SpaResult hc : spareSult) {
            List<SpaProperty> properties = hc.getProperty();

            AzimuthZenithAngle res = SpaUtils.getAzimuthAndZenithAngle(time, ZoneId.of("Asia/Shanghai"), Double.valueOf(properties.get(3).getPropertyValue())
                    , Double.valueOf(properties.get(4).getPropertyValue()), Double.valueOf(properties.get(2).getPropertyValue())
                    , Double.valueOf(properties.get(0).getPropertyValue()), Double.valueOf(properties.get(1).getPropertyValue()));

            if (hc.getProperty().size() == 7 ){
                properties.get(5).setPropertyValue(String.valueOf(90 - res.getZenithAngle()));
                properties.get(6).setPropertyValue(String.valueOf(res.getAzimuth()));

            }else {
                properties.add(new SpaProperty("Sun_Now_AL",String.valueOf(90 - res.getZenithAngle()) ,"当前太阳高度角"));
                properties.add(new SpaProperty("Sun_Now_AZ",String.valueOf(res.getAzimuth()) ,"当前太阳方位角"));
            }
            redisTemplate.opsForValue().set(hc.getItemCode(),hc.getProperty());
        }

        log.info("写入到redis完成，累计耗时：" + (System.currentTimeMillis()-start));



    }
}
